using Godot;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;

namespace Box;

public partial class NodeSelectPopupMenu : PopupMenu
{
	List<Action> Actions = new List<Action>(); 
	ProcessTreeGraphEditor edit;
	public override void _Ready()
	{
		edit = GetParent() as ProcessTreeGraphEditor;

		// Type node_control_type = typeof(ProcessNodeControl);
		// Assembly assembly = Assembly.GetExecutingAssembly();
		// foreach(Type type in assembly.GetTypes()) {
		// 	if(type.IsAssignableTo(node_control_type)) {
		// 		ProcessNodeControl node = Activator.CreateInstance(type) as ProcessNodeControl;
		// 		if(type.Name != "ProcessNodeControl") {
		// 			Actions.Add(()=> {
		// 				edit.AddProcessTreeNode(Activator.CreateInstance(type) as ProcessNodeControl);
						
		// 			});
		// 			AddItem(node.Title);
		// 		}
		// 	}
		// }

		var nodes = DirAccess.Open("res://source/process_tree_editor/node");
		var files = nodes.GetFiles();
		foreach(var file_name in files) {
			if(file_name.Find(".json") != -1) {
				Actions.Add(()=> {
					ProcessNodeControl control = new ProcessNodeControl();
					control.Setting($"res://source/process_tree_editor/node/{file_name}");
					edit.AddProcessTreeNode(control);
				});
				ProcessNodeControl control = new ProcessNodeControl();
				control.Setting($"res://source/process_tree_editor/node/{file_name}");
				AddItem(control.Title);
			}
			
		}

		MouseExited += () => { 
			Hide();
		};

		IdPressed += _id => {
			int id = ((int)_id);
			Actions[id]();
		};
	}
}
